﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Customer_Profile.eCard.DBDao;

namespace Customer_Profile.Admin
{
    public partial class UserAdd : System.Web.UI.Page
    {
        protected static int UserId;
        protected static int lvl;

        protected void Page_Load(object sender, EventArgs e)
        {
            // 在此处放置用户代码以初始化页面
            if (!Page.IsPostBack)
            {
                if (Request.QueryString["ID"] != null)
                    UserId = Convert.ToInt32(Request.QueryString["ID"].ToString());
                else
                {
                    KVB.Common.JavaScript.ViewMsgGoUrl("MenuManage.aspx", "You Must Select");
                }

                DataSet dsEntity = EntityDao.getEntityList("N");
                dropEntity.DataSource = dsEntity;
                dropEntity.DataTextField = "entity_name";
                dropEntity.DataValueField = "entity_id";
                dropEntity.DataBind();
            }
        }

        protected void ibtnAdd_ServerClick(object sender, EventArgs e)
        {
            string loginid = txtLoginID.Value;
            string name = txtUserName.Value;
            string password = txtUserPass.Value;
            string lvlType = rType.SelectedValue;
            string entity_id = dropEntity.SelectedItem.Value;
            string office = dropEntity.SelectedItem.Text;
            string mark = txtRemark.Value;
            string use = drpUsed.SelectedValue;

            UserInfoEntity UserE = new UserInfoEntity();
            UserE.UserLoginId = loginid;
            UserE.UserName = name;
            UserE.UserPassword = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password, "MD5").ToLower(); //password;
            UserE.UserlvlType = lvlType;
            UserE.UserEntityID = entity_id;
            UserE.UserOffice = office;
            UserE.UserRemark = mark;
            UserE.Used = use;
           
            Hashtable htUserInfo = new Hashtable();

            if (UserId == -1)
            {
                lvl = 0;
                UserE.ParentId = 0;
            }
            else
            {
                lvl = UserDao.getLvl(UserId);
                UserE.ParentId = UserId;
            }
            try
            {
                bool isSucess;
                if (UserId == -1)
                {
                    UserE.Lvl = 0;
                    htUserInfo.Add("UserInfo", UserE);
                    isSucess = UserDao.AddUser(htUserInfo);//, locationList);
                }
                else
                {
                    UserE.Lvl = lvl + 1;
                    htUserInfo.Add("UserInfo", UserE);
                    isSucess = UserDao.AddUser(htUserInfo);//, locationList);
                }
                if (isSucess == true)
                {
                    Response.Write("<script language=javascript>alert('Succeed!');</script>");
                }
            }
            catch(Exception ex)
            {
                KVB.Common.JavaScript.ViewAlertMsg(ex.ToString());
            }
        }

        protected void ibtnBack_ServerClick(object sender, EventArgs e)
        {
            Response.Redirect("UserManage.aspx", false);
        }
    }
}
